/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.player.ButtonComp');

goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventType');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyHandler');
goog.require('goog.events.KeyHandler.EventType');
goog.require('goog.ui.Component');
//goog.require('goog.ui.Control');



/**
 * @param {string=} opt_label A label to display. Defaults to "Click Me" if none
 *     provided.
 * @param {goog.dom.DomHelper=} opt_domHelper DOM helper to use.
 *
 * @extends {goog.ui.Component}
 * @constructor
 */
kramerius.audio.player.ButtonComp = function(opt_label, opt_domHelper){
    goog.ui.Component.call(this, opt_domHelper);
  
    /**
     * The label to display.
     * @type String
     * @private
     */
    this.initialLabel_ = opt_label || 'Click Me';

    /**
     * The current color.
     * @type String
     * @private
     */
    this.color_ = 'red';
  
    /**
     * Event handler for this object.
     * @type goog.events.EventHandler
     * @private
     */
    this.eh_ = new goog.events.EventHandler(this);
    
    /**
     * Keyboard handler for this object. This object is created once the 
     * component's DOM element is known.
     *
     * @type goog.events.KeyHandler|Null
     * @private
     */
    this.kh_ = null;
};
goog.inherits(kramerius.audio.player.ButtonComp, goog.ui.Component);
//goog.inherits(kramerius.audio.player.ButtonComp, goog.ui.Control);

/**
 * Creates an initial DOM representation for the component.
 * NUTNE IMPLEMENTOVAT  
 */
kramerius.audio.player.ButtonComp.prototype.createDom = function() {
    this.decorateInternal(this.dom_.createElement('div'));
};

/**
 * Creates an initial DOM representation for the component.
 * Defaultni implementace z Component nastavi jen 
 * this.element_ = element; 
 */
kramerius.audio.player.ButtonComp.prototype.decorateInternal = function(element) {
    this.setElementInternal(element);
    if (!this.getLabelText()) {
        this.setLabelText(this.initialLabel_);
    }
};

/**
 * Called when component's element is known to be in the document.
 * TODO: tady připojit listenery
 * Pokud bude implementovano, je treba taky implementovat exitDocument,
 * ktery uklidi listenery apod
 */
kramerius.audio.player.ButtonComp.prototype.enterDocument = function() {
    kramerius.audio.player.ButtonComp.superClass_.enterDocument.call(this);
    this.eh_.listen(this.getElement(), goog.events.EventType.CLICK,
        this.onDivClicked_);
};

/**
 * Called when component's element is known to have been removed from the
 * document.
 */
kramerius.audio.player.ButtonComp.prototype.exitDocument = function() {
    kramerius.audio.player.ButtonComp.superClass_.exitDocument.call(this);
    this.eh_.unlisten(this.getElement(), goog.events.EventType.CLICK,
        this.onDivClicked_);
};

/** @override */
kramerius.audio.player.ButtonComp.prototype.disposeInternal = function() {
  goog.demos.SampleComponent.superClass_.disposeInternal.call(this);
  this.eh_.dispose();
  if (this.kh_) {
    this.kh_.dispose();
  }
};


/**
 * Gets the current label text.
 * TODO: odstranit
 * @return {string} The current text set into the label, or empty string if
 *     none set.
 */
kramerius.audio.player.ButtonComp.prototype.getLabelText = function() {
    if (!this.getElement()) {
        return '';
    }
    return goog.dom.getTextContent(this.getElement());
};

/**
 * Sets the current label text. Has no effect if component is not rendered.
 * TODO: odstranit
 * @param {string} text The text to set as the label.
 */
kramerius.audio.player.ButtonComp.prototype.setLabelText = function(text) {
    if (this.getElement()) {
        goog.dom.setTextContent(this.getElement(), text);
    }
};




